<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/9
 * Time: 9:58
 */

namespace App\Repositories;

use App\Models\Study;
use App\Models\Project;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class StudyRepository extends BaseRepository
{
    public function model()
    {
        return Study::class;
    }

    public function stydyCount($paginate,$project_id,$company_id)
    {
        /**
         * 查询所有项目
         */
        $where['company_id'] = $company_id;
        if($project_id){
            $where['id'] = $project_id;
        }
        $projects = Project::query()
            ->select(['id', 'name'])
            ->withCount('users')
            ->withCount(['studies_type_1' => function ($q) {
                $q->where('type', 1);
            }])
            ->withCount(['studies_type_2' => function ($q) {
                $q->where('type', 2);
            }])
            ->where($where)
            ->paginate($paginate)->toArray();
        $project_id_arr = [];
        foreach ($projects['data'] as $k => $v){
            $project_id_arr[] = $v['id'];
        }
        /**
         * 查询每个项目每个分类的阅读人数
         */
        $read = DB::table('user_study_pivot')
            ->selectRaw('count(study_id) as count,project_id,type')
            ->whereIn('project_id',$project_id_arr)
            ->groupBy('project_id', 'type')
            ->get();

        /**
         * 遍历运算每个项目各个分类的阅读比例
         */
        foreach ($projects['data'] as $k => $v) {
            $projects['data'][$k]['key_person'] = 0;//关键人物阅读初始比例
            $projects['data'][$k]['train'] = 0;//岗位培训阅读初始比例
            $read_key_person = 0;//项目阅读关键人物的初始人数
            $read_train = 0;//项目阅读岗位培训的初始人数
            foreach ($read as $key => $val) {
                if ($val->project_id == $v['id']) {
                    if ($val->type == 1) {
                        $read_key_person += $val->count;
                    } else {
                        $read_train += $val->count;
                    }
                }
            }
            if ($read_key_person > 0 && $projects['data'][$k]['studies_type_1_count'] > 0) {
                $projects['data'][$k]['key_person'] = round($read_key_person / ($projects['data'][$k]['studies_type_1_count'] * $projects['data'][$k]['users_count']), 4) * 100;
            }
            if ($read_train > 0 && $projects['data'][$k]['studies_type_2_count'] > 0) {
                $projects['data'][$k]['train'] = round($read_train / ($projects['data'][$k]['studies_type_2_count'] * $projects['data'][$k]['users_count']), 4) * 100;
            }
        }
        return $projects;
    }

    public function read($user)
    {
        /**
         * 查询出两种培训资料的总数
         */
        $total_studies = DB::table('studies')
            ->selectRaw('count(id) as count,type')
            ->where('project_id', $user->project_id)
            ->groupBy('type')
            ->get();
        /**
         * 查询出所有培训资料
         */
        $studies_id = DB::table('studies')
            ->selectRaw('id,type')
            ->where('project_id', $user->project_id)
            ->orderBy('id', 'asc')
            ->get();
        /**
         * 查询出当前用户每种培训资料阅读的最大值id
         */
        $current = DB::table('user_study_pivot')
            ->where(['user_id' => $user->id, 'project_id' => $user->project_id])
            ->selectRaw('max(study_id) as mid,type')
            ->groupBy('type')
            ->get();


        foreach ($studies_id as $k => $v) {
            foreach ($total_studies as $key => $val) {
                if ($v->type == $val->type) {
                    $total_studies[$key]->total_id[] = $v->id;
                }
            }
        }

        foreach ($current as $k => $v) {
            foreach ($total_studies as $key => $val) {
                if ($v->type == $val->type) {
                    $total_studies[$key]->current = $v->mid;
                    $total_studies[$key]->current_index = array_search($v->mid, $total_studies[$key]->total_id) + 1;
                    $total_studies[$key]->suhedule = round($total_studies[$key]->current_index / $total_studies[$key]->count, 4) * 100;
                }
            }
        }

        return $total_studies;

    }

    public function countDetail($project_id,$paginate,$user_id)
    {
        /**
         * 查询所有用户
         */
        $where['project_id'] = $project_id;
        if($user_id){
            $where['id'] = $user_id;
        }
        $users = User::query()
            ->where($where)
            ->select(['id','name'])
            ->paginate($paginate)
            ->toArray();
        /**
         * 查询所有用户每个资料分类的阅读总数
         */
        $read = DB::table('user_study_pivot')
            ->where('project_id', $project_id)
            ->selectRaw('count(user_id) as count,type,user_id')
            ->groupBy('type', 'user_id')
            ->get();
        /**
         * 查询出两种培训资料的总数
         */
        $total_studies = DB::table('studies')
            ->selectRaw('count(id) as count,type')
            ->where('project_id', $project_id)
            ->groupBy('type')
            ->get();
        foreach ($users['data'] as $k => $v) {
            foreach ($total_studies as $key => $val){
                if($val->type==1){
                    $users['data'][$k]['key_person_count'] = $val->count;
                }else{
                    $users['data'][$k]['train_count'] = $val->count;
                }
            }
        }

        foreach ($users['data'] as $k => $v){
            $users['data'][$k]['key_person'] = 0;
            $users['data'][$k]['train'] = 0;
            foreach ($read as $key => $val)
            {
                if($val->user_id==$v['id']){
                    if($val->type==1){
                        $users['data'][$k]['key_person'] = round($val->count/$users['data'][$k]['key_person_count'],4)*100;
                    }else{
                        $users['data'][$k]['train'] = round($val->count/$users['data'][$k]['train_count'],4)*100;
                    }
                }
            }
        }
        return $users;
    }

    protected function serialization(array $attributes)
    {
        // TODO: Implement serialization() method.
    }
}